clear
*cd "/Users/connorforsythe/Library/CloudStorage/Box-Box/CMU/Marginal Mileage Project/Python Code/NHTSAnalysis"
cd "C:/Users/akshayaj/Box/NHTSAnalysis/"

//Always close log regardless of error
capture log close
log using "Logs/CausalNHTS.smcl", replace

local year = 2017






// local outcome_vars = "num_new_vehweighted single_vehicle_mean_ageweighted multi_vehicle_mean_ageweighted max_veh_ageweighted min_milesweighted multi_vehicle_num_vehiclesweight"
local outcome_vars = "single_vehicle_mean_ageweighted multi_vehicle_mean_ageweighted multi_vehicle_num_vehiclesweight num_new_vehweighted max_veh_ageweighted min_milesweighted num_vehiclesweighted num_newly_bought_vehweighted sum_total_milesweighted oldest_veh_travelweighted all_hh_new_vehweighted all_hh_newly_bought_vehweighted"

local i = 0
foreach year of num 2017 2009{
	clear
	
	if(`year'==2017){
		local ind_vars = "hhsizeweighted drvrcntweighted incomeweighted hbppopdnweighted wrkcountweighted numadltweighted youngchildweighted ageweighted"
	}
	else{
		local ind_vars = "hhsizeweighted drvrcntweighted incomeweighted hbppopdnweighted wrkcountweighted numadltweighted ageweighted"
	}
	
	import delimited "CleanData/NHTS`year'.csv"
	gen nosafetyweighted = 1-hassafetyweighted
	drop if treatmentyearweighted>=(`year'-2) & treatmentyearweighted<=(`year'+2)
	
	

// 	local treat_var = "hassafetyweighted"
	local treat_var = "nosafetyweighted"

	gen x = 0
	gen y = 0

	

	local j = 0
	local out_vars = ""
	
	reg y x 
	est sto model`i'
	
	foreach outcome_var in `outcome_vars'{
		
		if("`outcome_var'"=="num_new_vehweighted" | "`outcome_var'"=="all_hh_new_vehweighted" | "`outcome_var'"=="all_hh_newly_bought_vehweighted"){
			local floatFormat = "%4.3f"
		}
		else{
			local floatFormat = "%4.2f"
		}
		
		di "`outcome_var'"
		teffects psmatch (`outcome_var') (`treat_var' `ind_vars')
		di e(cmdline)
		ereturn list
		
		local tempMean = string(round(e(b)[1,1], .001), "`floatFormat'")
		local tempSD = string(round(sqrt(e(V)[1,1]), .001), "`floatFormat'")
		local tempT = e(b)[1,1]/sqrt(e(V)[1,1])
		local tempP = normal(-abs(`tempT'))*2
		
		if(`tempP'<0.01){
			local tempStars = "***"
		} 
		else if(`tempP'<0.05){
			local tempStars = "**"
		} 
		else if(`tempP'<0.1){
			local tempStars = "*"
		} 
		else{
			local tempStars = ""
		}
		
		
		
		local prefix = "\makecell{"
		local suffix = "}"
				
		local tempEntry = "`prefix'"+"`tempMean'`tempStars' \nextline (`tempSD')"+"`suffix'"
		est res model`i'
		estadd local var`j' = "`tempEntry'"
		local out_vars = "`out_vars' var`j'"
		local j = `j'+1
		
	}
	
	local i = `i'+1
}

esttab model1 model0 using "Tables/causal-NHTS.tex", drop(*) stats(`out_vars', labels("\makecell{Single-Vehicle Household \nextline Vehicle Age}" "\makecell{Multi-Vehicle Household \nextline Vehicle Age}" "\makecell{Multi-Vehicle Households \nextline Vehicle Count}" "\makecell{Vehicle-Owning Households \nextline New Vehicles Count}" "\makecell{Vehicle-Owning Households \nextline Maximum Vehicle Age}" "\makecell{Vehicle-Owning Households \nextline Minimum Self-Reported Annual VMT}" "\makecell{All Households \nextline Vehicle Count}" "\makecell{Vehicle-Owning Households \nextline Newly-Purchased Vehicle Count}" "\makecell{All Households \nextline Self-Reported Aggregate Household VMT}" "\makecell{Vehicle-Owning Households \nextline Oldest Vehicle Self-Reported Annual VMT}" "\makecell{All Households \nextline New Vehicles Count}" "\makecell{All Households \nextline Newly-Purchased Vehicle Count}")) nostar mtitles("NHTS 2009" "NHTS 2017") nonumbers not nolines  replace sub(\_ _) align(l|ll)
log close
cd "Logs"
translate "CausalNHTS.smcl" "CausalNHTS.pdf", replace
